package com.cn.ssm.permisson;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.util.Vector;
@Controller
public class PermissionManager {
    private Vector permission = null;
    public synchronized void setVector(Object v)
    {
        if (v == null)
        {
            v = new Vector();
        }
        permission = (Vector) v;
    }
    public synchronized boolean checkPermission(String PermissionCode)
    {
        int size = permission.size();
        for (int i = 0; i < size; i++)
        {
            try
            {
                HttpSession session = (HttpSession) permission.get(i);
                String mypermission = (String) session.getAttribute("permission_name"+PermissionCode);
                if (mypermission!=null && mypermission.length()>0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {

            }
        }
        return false;
    }
    public synchronized String getOnlineUser(int SequenceNum)
    {
        int size = permission.size();
        for (int i = 0; i < size; i++)
        {
            try
            {
                HttpSession session = (HttpSession) permission.get(i);
                String userName = (String) session.getAttribute("permission2_"+SequenceNum);
                if (userName != null && userName.length()>0)
                {
                    return userName;
                }
            }
            catch (Exception ex)
            {
                try
                {
                }
                catch (Exception e)
                {}
            }
        }
        return "";
    }
    public synchronized Vector addUser(Object v, HttpSession hs)
    {
        setVector(v);
        permission.add(hs);
        return permission;
    }
    public synchronized Vector removeUser(Object v, HttpSession hs)
    {
        setVector(v);
        try
        {
            permission.remove(hs);
        }
        catch (Exception e)
        {}
        return permission;
    }
}
